from django.db import models

# Create your models here.
"""
数据库字段模型
"""


class Test(models.Model):
    """
    自动创建数据库表结构,test表
    """
    # s_id = models.BigIntegerField()
    s_name = models.CharField(max_length=20)
    s_birth = models.DateField()
    s_sex = models.CharField(max_length=10)
    s_create = models.DateTimeField(auto_now_add=True)


class Contact(models.Model):
    name = models.CharField(max_length=50)
    age = models.IntegerField(default=0)
    email = models.EmailField()

    def __unicode__(self):
        return self.name


class Tag(models.Model):
    contact = models.ForeignKey(Contact, on_delete=models.CASCADE)  # CASCADE 级联删除
    tag_name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.tag_name


class Book(models.Model):
    """
    id:id
    title:书籍名称
    :param price 书籍价格
    :param pub_date 出版时间
    :param publish 出版社,外键
    :param authors 书作者
    """
    id = models.AutoField(primary_key=True)  # id 会自动创建,可以手动写入
    title = models.CharField(max_length=32)  # 书籍名称
    price = models.DecimalField(max_digits=5, decimal_places=2)  # 书籍价格
    pub_date = models.DateField()  # 出版时间
    publish = models.ForeignKey("Publish", on_delete=models.CASCADE)  # 出版社名称
    authors = models.ManyToManyField("Author")


class Publish(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=64)
    email = models.EmailField()


class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.SmallIntegerField()
    au_detail = models.OneToOneField("AuthorDetail", on_delete=models.CASCADE)


class AuthorDetail(models.Model):
    gender_choices = (
        (0, '女'),
        (1, '男'),
        (2, '保密'),
    )

    gender = models.SmallIntegerField(choices=gender_choices)
    tel = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    birthday = models.DateField()
